<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class UploadifyController extends CommonController {
    private $uploadir = 'Uploads';
    /**
    * 院校列表页
    * @date: 2017年8月2日 下午3:53:42
    */
    public function index(){
    	//初始化参数
		$size  = I('request.size',0);
		$num   = I('request.num',0);
		$input = I('request.input','');
		$area  = I('request.area','');
		$frame = I('request.frame','');
		$title = I('request.title','');
		$type  = I('request.type','');
		$desc  = I('request.desc','');
		$timestamp = time();
		$sessionid = session_id();
		$token = md5('unique_salt'.$timestamp);
		$this->assign('size',$size);
		$this->assign('num',$num);
		$this->assign('input',$input);
		$this->assign('area',$area);
		$this->assign('frame',$frame);
		$this->assign('title',$title);
		$this->assign('type',$type);
		$this->assign('desc',$desc);
		$this->assign('timestamp',$timestamp);
		$this->assign('sessionid',$sessionid);
		$this->assign('token',$token);
        $this->display();
        
    }
    
   	public function upload(){
   	    $verifyToken = md5('unique_salt' . I('request.timestamp'));
   	    $token = I('request.token','');
   	    $sessionid = I('request.sessionid','');   	    
   	    
        //判断上传状态
        if(!empty($_FILES) && $token==$verifyToken && isset($sessionid))
        {
            $tempfile      = $_FILES['Filedata'];
        	$tempfile_name = $tempfile['name'];
        	$tempfile_ext  = strtolower(substr(strrchr($tempfile_name, '.'), 1));
        	
        	$cfg_upload_img_type = explode('|', strtolower(C('cfg_upload_img_type')));
        	$cfg_upload_soft_type = explode('|', strtolower(C('cfg_upload_soft_type')));
        	$cfg_upload_media_type = explode('|', strtolower(C('cfg_upload_media_type')));
        	//检查文件类型,上传文件目录
        	if(in_array($tempfile_ext,$cfg_upload_img_type))
        	{
        	    $save_path = C('cfg_image_dir');
        	    $exts = $cfg_upload_img_type;
        	    $max_size = C('cfg_max_img_size');
        	}
        	else if(in_array($tempfile_ext, $cfg_upload_soft_type))
        	{
        	    $save_path = C('cfg_soft_dir');
        	    $exts = $cfg_upload_soft_type;
        	    $max_size = C('cfg_max_soft_size');
        	}
        	else if(in_array($tempfile_ext, $cfg_upload_media_type))
        	{
        	    $save_path = C('cfg_media_dir');
        	    $exts = $cfg_upload_media_type;
        	    $max_size = C('cfg_max_media_size');
        	}
        	else
        	{
        	    $this->errorReturn('您上传的文件类型为：['.$tempfile_ext.']，该文件类型不允许上传！');
        	}        	 
        	
        	$upload = new \Think\Upload();// 实例化上传类
        	$upload->maxSize   =     $max_size ;// 设置附件上传大小
        	$upload->exts      =     $exts;// 设置附件上传类型
        	$upload->rootPath  =     $this->uploadir.'/'; // 设置附件上传根目录
        	$upload->savePath  =     $save_path.'/'; // 设置附件上传（子）目录
        	$upload->subName   =     array('date','Ymd');
        	
        	// 上传文件
        	$info   =   $upload->upload();
        	if(!$info) {// 上传错误提示错误信息
        	    $this->errorReturn($upload->getError());
        	}else{// 上传成功
        	    $this->successReturn($info);
        	}
        }
        
        exit;
   	}
   	
   	public function del(){
   	    //删除元素
   	    $filename = I('request.filename','');
        //验证删除文件规则
        $match = "/^(".$this->uploadir.")\/(\w+)\/(\d+)\/(\w+)\.(\w{3})$/";
        $flag = preg_match($match, $filename);
        
        if($flag)
        {
            unlink(TRIO_ROOT .'/'. $filename);
        }
        exit();
   	}
   	
   	private function errorReturn($param) {
   	    echo '0,'.$param;
   	    exit;
   	}
   	
   	private function successReturn($param){
   	    $info = $param['Filedata'];
   	    $filename = $info['savename'];
   	    $tempfile_size = $info['size'];
   	    $save_url = $this->uploadir.'/'.$info['savepath'].$filename;
   	    $save_dir = $this->uploadir.'/'.$info['savepath'].$filename;
   	    echo implode(',', array($filename, $tempfile_size, $save_url, $save_dir));
   	    exit;
   	}
}